package IndusmecaSI.Indusmeca_Model
{
	import IndusmecaSI.Indusmeca_Type.Button;
	
	import flash.net.NetConnection;
	import flash.net.ObjectEncoding;
	import flash.net.Responder;
	
	import mx.collections.ArrayCollection;
	import mx.messaging.channels.StreamingAMFChannel;
	import IndusmecaSI.Indusmeca_Type.Button;
	
	
	public class Model_ListButton_Service
	{
		private var myServiceListButton:NetConnection;	
		private var _result:Function;
		private var _fault:Function;
		
		public function Model_ListButton_Service(result:Function, fault:Function)
		{
			_result = result;
			_fault = fault;
			myServiceListButton = new NetConnection();
			myServiceListButton.objectEncoding = ObjectEncoding.AMF3;
			myServiceListButton.connect("http://flex.jutech.lan/amfphp1.9/gateway.php");
		}
		
		public function getAllListbutton():void
		{
			var responder:Responder = new Responder(getAllListbutton_Result, onFault);
			myServiceListButton.call("INDUSMECA.ListbuttonService.getAllListbutton", responder);
		}
		public function getListbutton(id_item:int):void
		{
			var responder:Responder = new Responder(getListbutton_Result, onFault);
			myServiceListButton.call("INDUSMECA.ListbuttonService.getListbutton", responder, id_item);
		}
		public function getListbuttonByID(id_item:int):void
		{
			var responder:Responder = new Responder(getListbuttonByID_Result, onFault);
			myServiceListButton.call("INDUSMECA.ListbuttonService.getListbuttonByID", responder,id_item);
		}
		public function getListbuttonByIDenfant(id_itemSrc:int,id_item:int):void
		{
			var responder:Responder = new Responder(getListbuttonByIDenfant_Result, onFault);
			myServiceListButton.call("INDUSMECA.ListbuttonService.getListbuttonByIDenfant", responder,id_itemSrc,id_item);	
		}
		public function getAllListbutton_Result(result:Object):void
		{
			var res:ArrayCollection = new ArrayCollection();
			for each( var value:* in result)
			{
				res.addItem(miseEnFormButton(value));
			}
			_result(1,res);
		}
		public function getListbutton_Result(result:Object):void
		{
			var res:ArrayCollection = new ArrayCollection();
			for each( var value:* in result)
			{
				res.addItem(miseEnFormButton(value));
			}
			_result(2,res);
		}
		public function getListbuttonByID_Result(result:Object):void
		{
			var res:ArrayCollection = new ArrayCollection();
			for each( var value:* in result)
			{
				res.addItem(miseEnFormButton(value));
			}
			_result(3,res);
		}
		public function getListbuttonByIDenfant_Result(result:Object):void
		{
			var res:ArrayCollection = new ArrayCollection();
			for each( var value:* in result)
			{
				res.addItem(miseEnFormButton(value));
			}
			_result(4,res);
		}
		
		private function miseEnFormButton(value:Object):Button
		{
			var button:Button = new Button();
			
			button.button_id_flex = value.button_id_flex;
			button.uid =value.groupName;
			button.id =value.id_button;
			button.id_menu = value.id_menu;
			button.id_menuType = value.id_menuType;
			button.id_menuTypeEnfant = value.id_menuTypeEnfant;
			button.label = value.label_flex;
			button.name =value.stateName;
			button.placeBtnDepart = value.placeBtnDepart;
			
			return button;
		}
		
		public function onFault(obj : *):void{
			
				trace('ERREUR :'+obj.faultDetail + obj.faultString);
			_fault(obj.faultDetail +"\n\n"+obj.faultString);
		}
	}
}
